package com.google.android.gms.internal;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public final class lz {
    private final int amW;
    private final byte[] buffer;
    private int position;

    public static class a extends IOException {
        a(int i, int i2) {
            super("CodedOutputStream was writing to a flat byte array and ran out of space (pos " + i + " limit " + i2 + ").");
        }
    }

    private lz(byte[] bArr, int i, int i2) {
        this.buffer = bArr;
        this.position = i;
        this.amW = i + i2;
    }

    public static int E(long j) {
        return G(I(j));
    }

    public static int G(long j) {
        return (-128 & j) == 0 ? 1 : (-16384 & j) == 0 ? 2 : (-2097152 & j) == 0 ? 3 : (-268435456 & j) == 0 ? 4 : (-34359738368L & j) == 0 ? 5 : (-4398046511104L & j) == 0 ? 6 : (-562949953421312L & j) == 0 ? 7 : (-72057594037927936L & j) == 0 ? 8 : (Long.MIN_VALUE & j) == 0 ? 9 : 10;
    }

    public static long I(long j) {
        return (j << 1) ^ (j >> 63);
    }

    public static lz b(byte[] bArr, int i, int i2) {
        return new lz(bArr, i, i2);
    }

    public static int cz(String str) {
        try {
            byte[] bytes = str.getBytes("UTF-8");
            return bytes.length + eJ(bytes.length);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8 not supported.");
        }
    }

    public static int e(int i, long j) {
        return eH(i) + E(j);
    }

    public static int eE(int i) {
        return i >= 0 ? eJ(i) : 10;
    }

    public static int eH(int i) {
        return eJ(mh.u(i, 0));
    }

    public static int eJ(int i) {
        return (i & -128) == 0 ? 1 : (i & -16384) == 0 ? 2 : (-2097152 & i) == 0 ? 3 : (-268435456 & i) == 0 ? 4 : 5;
    }

    public static int h(int i, String str) {
        return eH(i) + cz(str);
    }

    public static int r(int i, int i2) {
        return eH(i) + eE(i2);
    }

    public void C(long j) throws IOException {
        F(I(j));
    }

    public void F(long j) throws IOException {
        while ((-128 & j) != 0) {
            eG((((int) j) & 127) | 128);
            j >>>= 7;
        }
        eG((int) j);
    }

    public void b(byte b) throws IOException {
        if (this.position == this.amW) {
            throw new a(this.position, this.amW);
        }
        byte[] bArr = this.buffer;
        int i = this.position;
        this.position = i + 1;
        bArr[i] = b;
    }

    public void b(int i, String str) throws IOException {
        t(i, 2);
        cy(str);
    }

    public void c(int i, long j) throws IOException {
        t(i, 0);
        C(j);
    }

    public void c(byte[] bArr, int i, int i2) throws IOException {
        if (this.amW - this.position >= i2) {
            System.arraycopy(bArr, i, this.buffer, this.position, i2);
            this.position += i2;
            return;
        }
        throw new a(this.position, this.amW);
    }

    public void cy(String str) throws IOException {
        byte[] bytes = str.getBytes("UTF-8");
        eI(bytes.length);
        t(bytes);
    }

    public void eC(int i) throws IOException {
        if (i >= 0) {
            eI(i);
        } else {
            F((long) i);
        }
    }

    public void eG(int i) throws IOException {
        b((byte) i);
    }

    public void eI(int i) throws IOException {
        while ((i & -128) != 0) {
            eG((i & 127) | 128);
            i >>>= 7;
        }
        eG(i);
    }

    public int nQ() {
        return this.amW - this.position;
    }

    public void nR() {
        if (nQ() != 0) {
            throw new IllegalStateException("Did not write as much data as expected.");
        }
    }

    public void p(int i, int i2) throws IOException {
        t(i, 0);
        eC(i2);
    }

    public void t(int i, int i2) throws IOException {
        eI(mh.u(i, i2));
    }

    public void t(byte[] bArr) throws IOException {
        c(bArr, 0, bArr.length);
    }
}
